﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace store.Areas.Home.Controllers
{
    public class ModulesController : Controller
    {
        private readonly storeEntities _db = new storeEntities();
        //
        // GET: /Home/Modules/

        [ChildActionOnly]
        public ActionResult Index(int id)
        {
            List<Product> list = null;
            Module module = _db.Modules.Find(id);
            if (module != null)
            {
                var document = XDocument.Parse(Server.HtmlDecode(module.Options));
                var categoryId = int.Parse(document.Root.Attribute("CategoryID").Value);
                var display = int.Parse(document.Root.Attribute("Display").Value);
                list = categoryId > 0 ? _db.Products.Where(x => x.CategoryID == categoryId).Take(display).ToList() : _db.Products.Take(display).ToList();
                ViewBag.Title = module.Title;
            }
            if (module.Postion == "center")
                return PartialView("Center", list);
            else
            {
                return null;
            }
        }

    }
}
